package com.speechb.modules.system.admin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.speechb.modules.system.admin.controller.base.BaseController;
import com.speechb.modules.system.admin.entity.SysMenu;
import com.speechb.modules.system.admin.service.ISysMenuService;
import com.speechb.modules.system.entity.CommonModel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author llj
 * @since 2019-10-03
 */
@RestController
@RequestMapping("${adminRoot}/sys_menu")
public class SysMenuController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(SysMenuController.class);
   @Autowired
    ISysMenuService sysMenuService;


   @GetMapping("")
    public void index() {
       String text = getPara("text");
       setData(sysMenuService.menus(text));
       renderUnCompressSuccess();
   }

    @GetMapping("/list")
    public void ordersList(){
        Integer pageNum = getPageNum();
        Integer pageSize = Integer.parseInt(getPara("page_size","0"));
        String order = getPara("order");
        String search = getPara("search");
        if (StringUtils.isNotEmpty(search)) {
            search = "%"+search+"%";
        }
        Page<Object> objects = PageHelper.startPage(pageNum, pageSize, true);
        List<SysMenu> listOfAdmin = sysMenuService.findListOfAdmin(order,search);
        writeData(listOfAdmin,(int)objects.getTotal());
    }

    @RequestMapping("/select")
    public void select() {
        String search = getPara("search");
        int rows = getInt("rows");
        int page = getInt("page");
        Page<Object> pageHelper = PageHelper.startPage(page, rows, true);
        List<SysMenu> sysMenuList = sysMenuService.searchSelect(search);
        setAttr("data", sysMenuList);
        setAttr("next_page", page + 1);
        setAttr("total", pageHelper.getTotal());
        renderUnCompressSuccess();
    }

    @GetMapping("sorting")
    public void sorting() {
       int parentId = getInt("id");
        SysMenu sysMenu = sysMenuService.findSortingByParentId(parentId);
        int sorting = 1;
        if (sysMenu != null) {
            sorting = sysMenu.getSorting() + 1;
        }
        setDataAndRender(sorting);
    }

    @PostMapping("add")
    public void add() {
        String menu = getPara("menu");
        String icon = getPara("icon");
        String url = getPara("url");
        String targetType = getPara("targetType");
        int sort = getInt("sort");
        int menuSelectId = getInt("menu_select");
        SysMenu sysMenu = new SysMenu();
        sysMenu.setText(menu);
        sysMenu.setIcon(icon);
        sysMenu.setParent_id(menuSelectId);
        sysMenu.setSorting(sort);
        sysMenu.setTargetType(targetType);
        sysMenu.setUrl(url);
        int sysMenuId = sysMenuService.add(sysMenu);
        if (sysMenuId >= 1) {
            render();
        } else {
            renderFail();
        }
    }

    @PostMapping("update")
    public void update() {
        int id = getInt("pk");
        String name = getPara("name");
        String value = getPara("value");
        String updateKeyName = name.replaceFirst(String.valueOf(id), "");
        CommonModel commonModel = new CommonModel();
        int updateSize = sysMenuService.updateSysMenu(id, updateKeyName, value);
        if (updateSize > 0) {
            render();
            return;
        }
        renderFail();
    }
}
